package com.pig4cloud.pig.module.controller.mer;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.mer.Mer;
import com.pig4cloud.pig.module.api.entity.mer.MerSelfVipGift;
import com.pig4cloud.pig.module.api.entity.mer.MerSelfVipLevel;
import com.pig4cloud.pig.module.api.entity.product.Product;
import com.pig4cloud.pig.module.service.mer.IMerSelfVipGiftService;
import com.pig4cloud.pig.module.service.mer.IMerSelfVipLevelService;
import com.pig4cloud.pig.module.service.mer.IMerService;
import com.pig4cloud.pig.module.service.product.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 会员赠品Controller
 *
 * @author ruoyi
 * @date 2023-06-09
 */
@Validated
@Api(value = "会员赠品控制器", tags = {"会员赠品管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/self/vip/gift")
public class MerSelfVipGiftController {

    private final IMerSelfVipGiftService merSelfVipGiftService;
    private final IMerService merService;
    private final IProductService productService;
    private final IMerSelfVipLevelService merSelfVipService;

    /**
     * 查询会员赠品列表
     */
    @ApiOperation("查询会员赠品列表")
    @PreAuthorize("@pms.hasPermission('module:gift:list')")
    @GetMapping("/list")
    public TableDataInfo<MerSelfVipGift> list(@Validated MerSelfVipGift merSelfVipGift) {
        QueryWrapper<MerSelfVipGift> qw = new QueryWrapper<>();
        if (merSelfVipGift.getMerId()!=null){
            qw.eq("mer_id",merSelfVipGift.getMerId());
        }
        if (merSelfVipGift.getMerVipId()!=null){
            qw.eq("mer_vip_id",merSelfVipGift.getMerVipId());
        }
        TableDataInfo<MerSelfVipGift> td = PageUtils.buildDataInfo(merSelfVipGiftService.page(PageUtils.buildPage(),qw));
        List<MerSelfVipGift> list = td.getData();
        for (MerSelfVipGift list1:list){
            if (ObjectUtil.isNotEmpty(list1.getMerId())){
                Mer mer = new Mer();
                mer=merService.getById(list1.getMerId());
                list1.setMerName(mer.getShopName());
            }
            if (ObjectUtil.isNotEmpty(list1.getProductId())){
                Product product = productService.getById(list1.getProductId());
                if (ObjectUtil.isNotEmpty(product)){
                    list1.setProductName(product.getStoreName());
                }

            }
            if (ObjectUtil.isNotEmpty(list1.getMerVipId())){
                MerSelfVipLevel merSelfVip = new MerSelfVipLevel();
                merSelfVip=merSelfVipService.getById(list1.getMerVipId());
                list1.setMerVipName(merSelfVip.getVipName());
            }
        }
        return td;
    }

    /**
     * 获取会员赠品详细信息
     */
    @ApiOperation("获取会员赠品详细信息")
    @PreAuthorize("@pms.hasPermission('module:gift:query')")
    @GetMapping("/{id}")
    public R<MerSelfVipGift> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        MerSelfVipGift merSelfVipGift = merSelfVipGiftService.getById(id);
        return R.ok(merSelfVipGift);
    }

    /**
     * 新增会员赠品
     */
    @ApiOperation("新增会员赠品")
    @PreAuthorize("@pms.hasPermission('module:gift:add')")
    @SysLog("会员赠品")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody MerSelfVipGift merSelfVipGift) {
        return R.okByRow(merSelfVipGiftService.save(merSelfVipGift) ? 1 : 0);
    }

    /**
     * 修改会员赠品
     */
    @ApiOperation("修改会员赠品")
    @PreAuthorize("@pms.hasPermission('module:gift:edit')")
    @SysLog("会员赠品")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody MerSelfVipGift merSelfVipGift) {
        return R.okByRow(merSelfVipGiftService.updateById(merSelfVipGift) ? 1 : 0);
    }

    /**
     * 删除会员赠品
     */
    @ApiOperation("删除会员赠品")
    @PreAuthorize("@pms.hasPermission('module:gift:remove')")
    @SysLog("会员赠品")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(merSelfVipGiftService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
